package com.ymatou.iapi.idcardquery.testcase;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.common.lang.CipherUtil;
import com.ymatou.iapi.idcardmng.parameter.ReceiveIdCardInfoBean;
import com.ymatou.iapi.idcardmng.service.ReceiveIdCardInfoCall;
import com.ymatou.iapi.idcardquery.parameter.GetManageBuyerIdCardInfoReq;
import com.ymatou.iapi.idcardquery.resp.IdCardInfo;
import com.ymatou.iapi.idcardquery.resp.IdCardInfoVerify;
import com.ymatou.iapi.idcardquery.resp.RejectReasonEnum;
import com.ymatou.iapi.idcardquery.service.GetManageBuyerIdCardInfoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.database.model.YmtIdCardInfo;
import com.ymttest.database.sqlwapper.YmtIdCardInfoWapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
/**
*根据买家id、姓名从本地库查询接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetManageBuyerIdCardInfo {
	private static GetManageBuyerIdCardInfoReq getmanagebuyeridcardinfoBean;
	private static GetManageBuyerIdCardInfoCall getmanagebuyeridcardinfoCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据买家id、姓名从本地库查询接口");
	}

	@Before
	public void caseUp() {
		getmanagebuyeridcardinfoBean = new GetManageBuyerIdCardInfoReq();
		getmanagebuyeridcardinfoCall = new GetManageBuyerIdCardInfoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getmanagebuyeridcardinfoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	/**
	 * 
	 * @param name
	 * @param idCode
	 * @param status  贝海状态（1. 通过审核，或未开始审核， 2. 审核不通过）
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	private void callReceiveIdCardInfoFromXlobo(String name, String idCode,
			int status) throws ClientProtocolException, IOException {

		ReceiveIdCardInfoBean receiveidcardinfoBean = new ReceiveIdCardInfoBean();
		ReceiveIdCardInfoCall receiveidcardinfoCall = new ReceiveIdCardInfoCall();

		receiveidcardinfoBean.setName(name);
		receiveidcardinfoBean.setIdCode(idCode);
		receiveidcardinfoBean.setStatus(status);
		receiveidcardinfoBean.setHasIdPic(true);
		receiveidcardinfoBean.setPhone("136" + TestDataManager.getRandomNum(8));
		receiveidcardinfoBean.setReverseSide("");
		receiveidcardinfoBean.setRightSide("");
		receiveidcardinfoBean.setBillCode(TestDataManager.getRandomNum(8));
		receiveidcardinfoBean.setOrderNo(TestDataManager.getRandomIntNum(8));

		receiveidcardinfoCall.setData(receiveidcardinfoBean);
		receiveidcardinfoCall.callService();

	}

	private void verifyDb() throws Exception {

		YmtIdCardInfoWapper ymtIdCard = new YmtIdCardInfoWapper();;
		
		String oldKey = "1234567890123456";
		String newKey = "123456789";
		String name = getmanagebuyeridcardinfoBean.getName();
		long buyerId = getmanagebuyeridcardinfoBean.getBuyerId();

		YmtIdCardInfo ymtIdCardInfo = ymtIdCard.selectByNameAndBuyerId(name,
				buyerId);

		IdCardInfo idCardInfo = getmanagebuyeridcardinfoCall
				.getManageBuyerIdCardInfoResp().getIdCardInfo();

		Logger.comment("校验身份证信息表ymt_idcardinfo");

		Logger.verifyEquals(ymtIdCardInfo.getCardId(), idCardInfo.getCardId(),
				"校验CardId");

		Logger.verifyEquals(ymtIdCardInfo.getName(),
				getmanagebuyeridcardinfoBean.getName(), "校验name");
		Logger.verifyEquals(CipherUtil.decryptAES(oldKey, ymtIdCardInfo.getIDCard()),
				idCardInfo.getCardNumber(), "校验CardNumber");

		Logger.verifyEquals(ymtIdCardInfo.getCardType(),
				idCardInfo.getCardType(), "校验CardType");

		Logger.verifyEquals(ymtIdCardInfo.getStatus(), idCardInfo.getStatus(),
				"校验Status");

		Logger.verifyEquals(ymtIdCardInfo.getHasPic(), idCardInfo.getHasPic(),
				"校验HasPic");

		Logger.verifyEquals(
				CipherUtil.decryptAES(newKey, ymtIdCardInfo.getMobile()),
				idCardInfo.getMobile(), "校验Mobile");
		String rejectReason = null;

		int rejectReasonCode = ymtIdCardInfo.getRejectReason();

		if (ymtIdCardInfo.getStatus() == -1) {

			RejectReasonEnum rejectReasonEnum = RejectReasonEnum

			.getEnum(rejectReasonCode);

			rejectReason = "审核未通过";

			if (null != rejectReasonEnum) {
				rejectReason = rejectReasonEnum.getMessage();
			}
		}

		Logger.verifyEquals(rejectReason, idCardInfo.getRejectReason(),
				"校验RejectReason");

		String expectedPicRightSide = "";

		String expectedPicReverseSide = "";

		if (!ymtIdCardInfo.getRightSide().isEmpty()) {

			String picUrl = IdCardInfoVerify.convertPicUrl(ymtIdCardInfo
					.getRightSide());

			expectedPicRightSide = IdCardInfoVerify.downloadPic(picUrl);

		}

		if (!ymtIdCardInfo.getReverseSide().isEmpty()) {

			String picUrl = IdCardInfoVerify.convertPicUrl(ymtIdCardInfo
					.getReverseSide());
			expectedPicReverseSide = IdCardInfoVerify.downloadPic(picUrl);
		}

		Logger.verifyEquals(expectedPicRightSide, idCardInfo.getPicRightSide(),
				"校验picRightSide");
		Logger.verifyEquals(expectedPicReverseSide,
				idCardInfo.getPicReverseSide(), "校验PicReverseSide");

		Logger.verifyEquals(ymtIdCardInfo.getCheckIdCardStatus(),
				idCardInfo.getCheckIdCardStatus(), "校验CheckIdCardStatus");

		DataUtil.verifyEquals(ymtIdCardInfo.getUploadTime(),
				idCardInfo.getUploadTime(), "验证UploadTime");

	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetManageBuyerIdCardInfo_001() {
		Logger.start(true, "根据买家id,姓名从本地库查询成功");
		try {
			long buyerId = Long.valueOf(EnvSetup.getData("SellerId2"));

			YmtIdCardInfoWapper ymtIdCard = new YmtIdCardInfoWapper();;

			YmtIdCardInfo ymtIdCardInfo = ymtIdCard.selectByBuyerId(buyerId);

			String name = ymtIdCardInfo.getName();

			getmanagebuyeridcardinfoBean.setBuyerId(buyerId);
			getmanagebuyeridcardinfoBean.setName(name);

			getmanagebuyeridcardinfoCall.setData(getmanagebuyeridcardinfoBean);
			getmanagebuyeridcardinfoCall.callService();

			Logger.verifyEquals("200",
					getmanagebuyeridcardinfoCall.getString("code"), "验证Code");

			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetManageBuyerIdCardInfo_002() {
		Logger.start(true, "本地没有身份证记录,接口返回空");
		try {

			getmanagebuyeridcardinfoBean.setBuyerId(288483L);
			getmanagebuyeridcardinfoBean.setName("L");

			getmanagebuyeridcardinfoCall.setData(getmanagebuyeridcardinfoBean);
			getmanagebuyeridcardinfoCall.callService();

			Logger.verifyEquals("200",
					getmanagebuyeridcardinfoCall.getString("code"), "验证Code");

			Logger.verifyIsNull(getmanagebuyeridcardinfoCall
					.getManageBuyerIdCardInfoResp().getIdCardInfo(),
					"校验接口返回 IdCardInfo为null");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetManageBuyerIdCardInfo_003() {
		Logger.start(true, "buyerid,与name不匹配.接口返回为null");
		try {

			getmanagebuyeridcardinfoBean.setBuyerId(288484L);
			getmanagebuyeridcardinfoBean.setName("L");

			getmanagebuyeridcardinfoCall.setData(getmanagebuyeridcardinfoBean);
			getmanagebuyeridcardinfoCall.callService();

			Logger.verifyEquals("200",
					getmanagebuyeridcardinfoCall.getString("code"), "验证Code");

			Logger.verifyIsNull(getmanagebuyeridcardinfoCall
					.getManageBuyerIdCardInfoResp().getIdCardInfo(),
					"校验接口返回 IdCardInfo为null");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetManageBuyerIdCardInfo_004() {
		Logger.start(true, "买家Id为null");
		try {

			//getmanagebuyeridcardinfoBean.setBuyerId(288483L);
			getmanagebuyeridcardinfoBean.setName("L");

			getmanagebuyeridcardinfoCall.setData(getmanagebuyeridcardinfoBean);
			getmanagebuyeridcardinfoCall.callService();

			Logger.verifyEquals("400",
					getmanagebuyeridcardinfoCall.getString("code"), "验证code");
			Logger.verifyEquals("100",
					getmanagebuyeridcardinfoCall.getString("bcode"), "验证bcode");
			Logger.verifyEquals("buyerId:buyerId未提供",
					getmanagebuyeridcardinfoCall.getString("msg"), "验证msg");

			Logger.verifyIsNull(getmanagebuyeridcardinfoCall
					.getManageBuyerIdCardInfoResp().getIdCardInfo(),
					"校验接口返回 IdCardInfo为null");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetManageBuyerIdCardInfo_005() {
		Logger.start(true, "收件人为null");
		try {

			getmanagebuyeridcardinfoBean.setBuyerId(288483L);
			//getmanagebuyeridcardinfoBean.setName("L");

			getmanagebuyeridcardinfoCall.setData(getmanagebuyeridcardinfoBean);
			getmanagebuyeridcardinfoCall.callService();

			Logger.verifyEquals("400",
					getmanagebuyeridcardinfoCall.getString("code"), "验证code");
			Logger.verifyEquals("100",
					getmanagebuyeridcardinfoCall.getString("bcode"), "验证bcode");
			Logger.verifyEquals("name:收件人未提供",
					getmanagebuyeridcardinfoCall.getString("msg"), "验证msg");

			Logger.verifyIsNull(getmanagebuyeridcardinfoCall
					.getManageBuyerIdCardInfoResp().getIdCardInfo(),
					"校验接口返回 IdCardInfo为null");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetManageBuyerIdCardInfo_006() {
		Logger.start(true, "根据买家id,姓名从本地库查询成功");
		try {
			long buyerId = Long.valueOf(EnvSetup.getData("SellerId2"));

			//YmtIdCardInfoWapper ymtIdCard = new YmtIdCardInfoWapper();;

			//YmtIdCardInfo ymtIdCardInfo = ymtIdCard.selectByBuyerId(buyerId);

			String name = "周颂";

			getmanagebuyeridcardinfoBean.setBuyerId(buyerId);
			getmanagebuyeridcardinfoBean.setName(name);

			getmanagebuyeridcardinfoCall.setData(getmanagebuyeridcardinfoBean);
			getmanagebuyeridcardinfoCall.callService();

			Logger.verifyEquals("200",
					getmanagebuyeridcardinfoCall.getString("code"), "验证Code");

			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetManageBuyerIdCardInfo_007() {
		Logger.start(true, "贝海审核通过status=1，根据买家id,姓名从本地库查询成功");
		try {
			long buyerId = Long.valueOf(EnvSetup.getData("SellerId2"));

			//YmtIdCardInfoWapper ymtIdCard = new YmtIdCardInfoWapper();;

			//YmtIdCardInfo ymtIdCardInfo = ymtIdCard.selectByBuyerId(buyerId);

			String name = "周颂";
			String idCode = "4914553520112944608";

			callReceiveIdCardInfoFromXlobo(name, idCode, 1);

			getmanagebuyeridcardinfoBean.setBuyerId(buyerId);
			getmanagebuyeridcardinfoBean.setName(name);

			getmanagebuyeridcardinfoCall.setData(getmanagebuyeridcardinfoBean);
			getmanagebuyeridcardinfoCall.callService();

			Logger.verifyEquals("200",
					getmanagebuyeridcardinfoCall.getString("code"), "验证Code");

			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetManageBuyerIdCardInfo_008() {
		Logger.start(true, "贝海审核不通过 status=2，根据买家id,姓名从本地库查询成功");
		try {
			long buyerId = Long.valueOf(EnvSetup.getData("SellerId2"));

			//YmtIdCardInfoWapper ymtIdCard = new YmtIdCardInfoWapper();;

			//YmtIdCardInfo ymtIdCardInfo = ymtIdCard.selectByBuyerId(buyerId);

			String name = "周颂";
			String idCode = "4914553520112944608";

			callReceiveIdCardInfoFromXlobo(name, idCode, 2);

			getmanagebuyeridcardinfoBean.setBuyerId(buyerId);
			getmanagebuyeridcardinfoBean.setName(name);

			getmanagebuyeridcardinfoCall.setData(getmanagebuyeridcardinfoBean);
			getmanagebuyeridcardinfoCall.callService();

			Logger.verifyEquals("200",
					getmanagebuyeridcardinfoCall.getString("code"), "验证Code");

			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetManageBuyerIdCardInfo_009() {
		Logger.start(true, "贝海审核不通过 status=3，根据买家id,姓名从本地库查询成功");
		try {
			long buyerId = Long.valueOf(EnvSetup.getData("SellerId2"));

			//YmtIdCardInfoWapper ymtIdCard = new YmtIdCardInfoWapper();;

			//YmtIdCardInfo ymtIdCardInfo = ymtIdCard.selectByBuyerId(buyerId);

			String name = "周颂";
			String idCode = "4914553520112944608";

			callReceiveIdCardInfoFromXlobo(name, idCode, 3);

			getmanagebuyeridcardinfoBean.setBuyerId(buyerId);
			getmanagebuyeridcardinfoBean.setName(name);

			getmanagebuyeridcardinfoCall.setData(getmanagebuyeridcardinfoBean);
			getmanagebuyeridcardinfoCall.callService();

			Logger.verifyEquals("200",
					getmanagebuyeridcardinfoCall.getString("code"), "验证Code");

			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetManageBuyerIdCardInfo_010() {
		Logger.start(true, "贝海审核不通过 status=4，根据买家id,姓名从本地库查询成功");
		try {
			long buyerId = Long.valueOf(EnvSetup.getData("SellerId2"));

			//YmtIdCardInfoWapper ymtIdCard = new YmtIdCardInfoWapper();;

			//YmtIdCardInfo ymtIdCardInfo = ymtIdCard.selectByBuyerId(buyerId);

			String name = "周颂";
			String idCode = "4914553520112944608";

			callReceiveIdCardInfoFromXlobo(name, idCode, 4);

			getmanagebuyeridcardinfoBean.setBuyerId(buyerId);
			getmanagebuyeridcardinfoBean.setName(name);

			getmanagebuyeridcardinfoCall.setData(getmanagebuyeridcardinfoBean);
			getmanagebuyeridcardinfoCall.callService();

			Logger.verifyEquals("200",
					getmanagebuyeridcardinfoCall.getString("code"), "验证Code");

			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	/*public static void main(String[] args) throws Exception {
		String key = "1234567890123456";
		System.out.println(CipherUtil.decryptAES(key,
				"5145A97BF94C64C747928C03DA43EDBA349F4E87EFAFBFF6F7800822C7B1E585FAE568836EEEF4673518AC12D1437DBE9D8D1F180848CECC32C982596A3CF1C0D1A36D51C72E151EAA120BA348AF166297CEE5B9C1EBA769CDD9998FFA74881E"));
	}*/
}